<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeuserController extends Controller
{   
    //前台个人中心页面
   public function getHomeuser()
   {	
   		//获取用户id
   		$id = session('id');
      //查询收货人信息
      $address = DB::table('address')->where('uid',$id)->get();
   		//查询个人信息
   		$users = DB::table('users')->where('id',$id)->first();
   		//查询订单数量
        $num = DB::table('order')->where('uid',$id)->count();
        // dd($num);
        //解析模板
        return view('/home/homeuser/homeuser',['users'=>$users,'num'=>$num,'address'=>$address]);
   }

    //前台个人中心个人资料页面
    public function getHomeuserdata()
    {  
        //获取用户id
        $id = session('id');
        //查询个人信息
        $users = DB::table('users')->where('id',$id)->first();
        //解析模板
        return view('/home/homeuser/homeuserdata',['users'=>$users]);
    }

    //执行修改
    public function postDoedit(Request $request)
    {
        
        $data = $request->except('_token');
        // dd($data);
        //获取用户id
        $id = session('id');
        //处理图片上传
        if($request->hasFile('pic')){
            $data['pic'] = UserController::upload($request);
        }else{
            $data['pic'] = DB::table('users')->where('id',$id)->first()->pic;
        }
        // dd($data);
        //执行插入
        $res = DB::table('users')->where('id',$id)->update($data);
        // 判断
        if($res)
        {
            return redirect('/homeuser/homeuser')->with('success','添加成功');
        }else
        {
            return back()->with('error','添加失败');
        }
    }


    //前台个人中心修改密码页面
    public function getHomeuserpass()
    {  
        //解析模板
        return view('/home/homeuser/homeuserpass');
    }

    //执行密码修改
    public function postDoeditpass(Request $request)
    {
        // dd($request->all());
        //获取用户id
        $id = session('id');
        //根据ID查询用户原密码
        $user = DB::table('users')->where('id',$id)->first();
        // dd($user);
        // dd($request->input('repass'));
        //判断原密码是否正确
        if(Hash::check($request->input('ypass'),$user->upass)){
           if($request->input('xpass') != $request->input('repass')){
              return back()->with('error','两次输入的新密码不一致');
           }   
        }else{
          //原密码不匹配
          return back()->with('error','原密码不正确');
        }

        $upass = [];
        //提取数据  密码加密
        $upass['upass'] = Hash::make($request->input('xpass'));
        
        // dd($upass);
        //插入数据
        $res = DB::table('users')->where('id',$id)->update($upass);

        if($res){
            $request->session()->flush();
            return redirect('/account')->with('success','修改成功,请从新登录');
        }

    }

    //前台个人中心收货地址页面
    public function getHomeuseraddress()
    {  
      //获取用户id
      $id = session('id');
      //查询收货人信息
      $address = DB::table('address')->where('uid',$id)->get();
      // dd($address);
      //解析模板
      return view('/home/homeuser/homeuseraddress',['address'=>$address]);
    }

   //个人中心添加地址
   public function postInsert(Request $request)
   {	
   		// dd($request->all());
   		//提取数据
   		$data = $request->except('_token');
   		$data['uid'] = session('id');
   		// dd($data);
   		//添加到数据库
   		$res = DB::table('address')->insert($data);

   		return redirect('/homeuser/homeuseraddress');
   }

   //个人中心 收货地址修改页面
   public function getAddressedit(Request $request)
   {
      // dd($request->all());
      //获取ID
      $id = $request->input('id');
      //根据id查询地址
      $address = DB::table('address')->where('id',$id)->first();
      // dd($address);

      return view('/home/homeuser/homeuseraddressedit',['address'=>$address]);
   }

   //执行收货地修改
   public function postAddressdoedit(Request $request)
   {
      // dd($request->all());
      //获取地址ID
      $id = $request->input('id');
      // dd($id);
      //提取数据
      $data = $request->except('_token','id');
      $data['uid'] = session('id');
      // dd($data,$id);
      //添加到数据库
      $res = DB::table('address')->where('id',$id)->update($data);
      // 判断
      if($res)
      {
         return redirect('/homeuser/homeuseraddress')->with('success','添加成功');
      }else
      {
          return back()->with('error','添加失败');
      }
   }

   //收货地址删除
   public function getAddressdelete(Request $request)
   {
      // dd($request->all());
      //获取id
      $id = $request->input('id');
      //根据id执行删除
      $res = DB::table('address')->where('id',$id)->delete();
      //判断
      if($res)
      {
         return redirect('/homeuser/homeuseraddress')->with('success','删除成功');
      }else
      {
          return back()->with('error','删除失败');
      }
   }

}
